package demo100;

import java.util.HashSet;
import java.util.Set;

public class longestConsecutive {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for(int x:nums) {
            set.add(x);
        }
        int max = 0;
        for(int x:set) {
            if(!set.contains(x-1)) {
                int currentNum = x;
                int len = 1;
                while(set.contains(currentNum+1)) {
                    currentNum+=1;
                    len++;
                }
                if(max < len) {
                    max = len;
                }
            }
        }
        return max;
    }
}
